package com.hm.health.widget;

import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;

import com.hm.health.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenshi on 2020/3/4.
 */

public class DeviceFilterPopWindow {
    private PopupWindow mPopWindow;
    private Activity mContext;
    private View mRootView;

    @BindView(R.id.edit_name_filter)
    EditText mEditNameFilter;
    @BindView(R.id.edit_advertising_filter)
    EditText mEditAdvertFilter;
    @BindView(R.id.seek_bar_rssi_value)
    SeekBar mSeekBarRss;
    @BindView(R.id.tv_rssi_value)
    TextView mTvRssValue;
    @BindView(R.id.view_mask)
    View mViewMark;

    private Callback mCallback;
    private String mNameFilter = "";
    private String mAdvertisingFilter = "";
    private int mRssi = -100;

    public interface Callback {
        void onFilterChanged(String nameFilter, String advertisingFilter, int rssi);
    }

    public DeviceFilterPopWindow(Activity context, View view) {
        mContext = context;
        mRootView = LayoutInflater.from(mContext).inflate(R.layout.layout_device_filter, null);
        ButterKnife.bind(this, mRootView);
        initView(mRootView);
        initPopWindow(mRootView, view);
    }

    @OnClick(R.id.img_name_delete)
    void onNameDeleteClicked(View v) {
        mNameFilter = "";
        mEditNameFilter.setText("");
        updateResult();
    }

    @OnClick(R.id.img_adv_delete)
    void onAdvDeleteClicked(View v) {
        mAdvertisingFilter = "";
        mEditAdvertFilter.setText("");
        updateResult();
    }

    public void removeAllFilter() {
        mRssi = -100;
        mTvRssValue.setText(String.valueOf(mRssi));
        onAdvDeleteClicked(null);
        onNameDeleteClicked(null);
    }

    private void updateResult() {
        if (null != mCallback) {
            mCallback.onFilterChanged(mNameFilter, mAdvertisingFilter, mRssi);
        }
    }

    private void initView(View rootView) {

        mEditNameFilter.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                mNameFilter = charSequence.toString();
                updateResult();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        mEditAdvertFilter.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                mAdvertisingFilter = charSequence.toString();
                updateResult();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        mSeekBarRss.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                if (b) {
                    mTvRssValue.setText(String.valueOf(i - 100));
                    mRssi = Integer.decode(mTvRssValue.getText().toString());
                    updateResult();
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        mViewMark.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPopWindow.dismiss();
            }
        });
    }

    public void show(String nameFilter, String advertisingFilter, int rssi, Callback callback) {
        mCallback = callback;
        mNameFilter = nameFilter;
        mAdvertisingFilter = advertisingFilter;
        mRssi = rssi;
        mEditAdvertFilter.setText(advertisingFilter);
        mEditNameFilter.setText(nameFilter);
        mSeekBarRss.setProgress(Math.abs(100 + rssi));
        mTvRssValue.setText(String.valueOf(rssi));
    }

    private void initPopWindow(View popView, View view) {
        mPopWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, true);
        mPopWindow.setTouchable(true);
        mPopWindow.setTouchInterceptor((v, event) -> false);
        mPopWindow.showAsDropDown(view, 0, 0, Gravity.BOTTOM);
    }
}
